'use strict'

const bcrypt = require('bcrypt-promise')
const moment = require('moment')
// const fs = require('fs-extra')
// const mime = require('mime-types')
const sqlite = require('sqlite-async')
// const query = require('../config/connection');
const saltRounds = 10

// {id, user_id, score, create_time, update_time}
module.exports = class AnswerLog {
	constructor(dbName = 'website.db') {
		return (async() => {
			this.db = await sqlite.open(dbName)
			return this
		})()
	}
	async listALl() {
		try {
			let sql = `SELECT * FROM answerlogs;`
			const data = await this.db.all(sql)
			return data
		} catch(err) {
			throw err
		}
	}

	async getOne(id) {
		try {
			let sql = `SELECT * FROM answerlogs where id = ${id};`
			const data = await this.db.get(sql)
			return data
		} catch(err) {
			throw err
		}
	}

	async listAnswerLogByUserId(userId) {
		try {
			let sql = `SELECT * FROM answerlogs where user_id = ${userId};`
			const data = await this.db.all(sql)
			data.forEach(element => {
				element.create_time =  moment().format('MMMM Do YYYY, h:mm:ss a');
			});
			return data
		} catch(err) {
			throw err
		}
	}

	async add(userId, finalScore, isSuccessful) {
		try {
			let sql = `INSERT INTO answerlogs(user_id, final_score, is_success) VALUES(${userId}, ${finalScore}, ${isSuccessful})`
			await this.db.run(sql)
			return true
		} catch(err) {
			throw err
		}
	}
}
